//package com.cssy.yd.basic.web;
//
//import java.util.List;
//import java.util.Map;
//
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import org.springframework.stereotype.Controller;
//import org.springframework.ui.Model;
//import org.springframework.validation.BindingResult;
//import org.springframework.web.bind.annotation.PathVariable;
//import org.springframework.web.bind.annotation.RequestMapping;
//
//import com.alibaba.fastjson.JSONArray;
//import com.cssy.base.web.BaseController;
//import com.cssy.base.web.BodyMsg;
//import com.cssy.yd.basic.entity.BasicAreaDO;
//import com.cssy.yd.basic.entity.BasicBomDO;
//import com.cssy.yd.basic.entity.BasicBomRalDO;
//import com.cssy.yd.basic.entity.BasicFundamentalUnitDO;
//import com.cssy.yd.basic.entity.BasicMaterialsDO;
//import com.cssy.yd.basic.entity.BasicMaterialstypeDO;
//import com.cssy.yd.basic.entity.BasicMeasurementUnitDO;
//import com.cssy.yd.basic.service.IBasicFundamentalUnitService;
//import com.cssy.yd.basic.service.IBasicMaterialsService;
//import com.cssy.yd.basic.service.IBasicMeasurementUnitService;
//import com.plat.exception.SyBaseException;
//import com.plat.helper.JqGridHandler;
//import com.plat.helper.SearchCondition;
//import com.plat.helper.SearchConditions;
//import com.plat.helper.SearchResult;
//import com.plat.util.GlobalResLoader;
//import com.plat.util.IDCodeTypeUtil;
//import com.plat.util.OperationFlag;
//import com.plat.util.SessionUtil;
//import com.plat.util.json.AjaxUtil;
//import com.plat.util.json.OutJsonGrid;
//import com.yuendong.sy.po.code.SyCode;
//import com.yuendong.sy.service.code.SyCodeType;
//import com.yuendong.sy.service.code.impl.SyCodeService;
//
//@Controller
//@RequestMapping("/yd/basic/measurementUnit")
//public class BasicMeasurementUnitController extends BaseController<BasicMeasurementUnitDO>{
//	@Resource(name="syCodeService")
//	private SyCodeService syCodeService;
//	
//	@Resource(name="basicMeasurementUnitService")
//	private IBasicMeasurementUnitService basicMeasurementUnitService;
//	
//	@Resource(name="basicFundamentalUnitService")
//	private IBasicFundamentalUnitService basicFundamentalUnitService;
//	
//	@Resource(name="basicMaterialsService")
//	private IBasicMaterialsService basicMaterialsService;
//	
//	private static final String MANAGER = "/yd/basic/measurementUnit/MeasurementUnitManager";//列表页面
//	private static final String EIDT = "/yd/basic/measurementUnit/MeasurementUnitOperation";//添加、修改、显示页面	
//	@Override
//	public String manager(HttpServletRequest request, Model model)
//			throws Exception {
//		// TODO Auto-generated method stub
//		return MANAGER;
//	}
//
//	@Override
//	public void list(HttpServletRequest request, HttpServletResponse response)
//			throws Exception {
//		JqGridHandler hander=new JqGridHandler(request);
//		SearchConditions scs = new SearchConditions();
//		hander.getWheres(scs);	
////		scs.setRemovedProperty("removed");//判断是否有删除标识
//		String ID =request.getParameter("ID");
//		if(ID!=null&&!"".equals(ID)){
//			scs.addCondition("materialsid", ID,SearchCondition.OP_EQUAL);
//		}
//		scs.setOrderBy("ifnull(default_unit,5) asc");
//		SearchResult result=	basicMeasurementUnitService.findListByExample(scs);
//		OutJsonGrid out=new OutJsonGrid(result);
//		out.printJSon(response);
//		
//	}
//
//	@Override
//	public String operationForm(HttpServletRequest request, @PathVariable String id,
//			@PathVariable String flag, Model model) throws Exception {
//		SearchConditions scs3 = new SearchConditions();
//		List<BasicFundamentalUnitDO> bfu =  basicFundamentalUnitService.findListByExample(scs3).getResults();
//		if(OperationFlag.ADD.equals(flag)){//我新增加时操作
//			BasicMaterialsDO bm =basicMaterialsService.findListById(id);
//			JqGridHandler hander=new JqGridHandler(request);
//			SearchConditions scs = new SearchConditions();
//			scs.addCondition("materialsid", bm.getId(),SearchCondition.OP_EQUAL);
//			scs.addCondition("default_unit", "0",SearchCondition.OP_EQUAL);
//			hander.getWheres(scs);	
//			SearchResult result=	basicMeasurementUnitService.findListByExample(scs);
//			model.addAttribute("mid", bm.getId());
//			model.addAttribute("mname", bm.getName());
//			model.addAttribute("mcode", bm.getCode());
//			model.addAttribute("flag", "add");
//			model.addAttribute("count", result.getResults().size());
//		}else{
//			BasicMeasurementUnitDO bmu=	basicMeasurementUnitService.findListById(id);
//			model.addAttribute("bmu", bmu);
//			model.addAttribute("flag", "edit");
//		}
//		model.addAttribute("bfu", bfu);
//		return EIDT;
//	}
//
//	@Override
//	public Map<String, Object> save(HttpServletRequest request,
//			BasicMeasurementUnitDO curAP, BindingResult result, Model model)
//			throws Exception {
//		BodyMsg bodyMap=BodyMsg.getInstance();
//		String materialsid  = request.getParameter("materialsidadd");
//		String materialsname = request.getParameter("materialsnameadd");
//		String materialscode = request.getParameter("materialscodeadd");
//		try {
//			if(curAP.getId()!=null && !"".equals(curAP.getId())){//修改
//				basicMeasurementUnitService.update(curAP, SessionUtil.getUser(request));
//				bodyMap.isSuccess();
//				bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
//				bodyMap.configWebId(curAP.getId());
//			}else{//新增
//				if(curAP.getUnitname() != null && curAP.getUnitname().length() > 0){
//					JqGridHandler hander=new JqGridHandler(request);
////					SearchConditions scs = new SearchConditions();
////					scs.addCondition("unitname", curAP.getUnitname(),SearchCondition.OP_EQUAL);
////					scs.addCondition("unitname", curAP.getUnitname(),SearchCondition.OP_EQUAL);
////					hander.getWheres(scs);	
//					List<BasicMeasurementUnitDO> list =	basicMeasurementUnitService.findUnit(materialsid);
//					for (BasicMeasurementUnitDO basicMeasurementUnitDO : list) {
//						if(basicMeasurementUnitDO.getUnitname().equals(curAP.getUnitname())){
//							bodyMap.isFail();
//							bodyMap.configWebInfor("对不起您输入的单位名称已存在");
//							return bodyMap.modelMap;
//						}
//					}
//				}
//				if(curAP.getUnitcode()!=null&&!"".equals(curAP.getUnitcode())){
//					BasicMeasurementUnitDO bdo = new BasicMeasurementUnitDO();
//					bdo.setMaterialsid(materialsid);
//					bdo.setUnitcode(curAP.getUnitcode());
//					List<BasicMeasurementUnitDO> codeInfo = basicMeasurementUnitService.findListByExample(bdo);
//					curAP.setRemoved(0);
//					boolean flag = false; 
//					if(codeInfo!=null && !codeInfo.isEmpty()){
//						for (BasicMeasurementUnitDO ba : codeInfo ){
//							if(ba.getUnitcode().equals(bdo.getUnitcode())){
//								flag = true;
//							}
//						}
//					}
//					if (flag){
//							bodyMap.isFail();
//							bodyMap.configWebInfor("对不起您输入的编码已存在");
//							return bodyMap.modelMap;
//					}
//				}else{
//					curAP.setUnitcode(syCodeService.getCode("UNIT","UNIT",SyCode.DATE_YYMM,5));
//				}
//				curAP.setMaterialsid(materialsid);
//				curAP.setMaterialscode(materialscode);
//				curAP.setMaterialsname(materialsname);
//				BasicMeasurementUnitDO cdo=basicMeasurementUnitService.save(curAP, SessionUtil.getUser(request));
//				bodyMap.isSuccess();
//				bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
//				bodyMap.configWebId(cdo.getId());
//			}
//		} catch (Exception e) {			
//			e.printStackTrace();
//			bodyMap.isFail();
//			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()+e.getMessage());
//		}
//		return bodyMap.modelMap;
//	}
//
//	@Override
//	public Map<String, Object> delete(HttpServletRequest request,
//			HttpServletResponse response, Model model) throws Exception {
//		BodyMsg bodyMap=BodyMsg.getInstance();
//		try{
//			String ids = request.getParameter("ids");
//			String[] fields=ids.split(",");
//			
//			for(String field : fields){
//				basicMeasurementUnitService.delete(field, SessionUtil.getUser(request));
//			}
//		
//			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
//		}catch(Exception e){
//			e.printStackTrace();
//			bodyMap.isFail(GlobalResLoader.getOpfailed()+e.getMessage());
//		}
//		return bodyMap.modelMap;
//	}
//	@RequestMapping(value = "/tree")
//	public void treelist(HttpServletRequest request,HttpServletResponse response)throws Exception{
//		String tr=null;
//		SearchConditions scs = new SearchConditions();
//		scs.setOrderBy("code asc");
//		List<BasicMaterialsDO> list =basicMaterialsService.findListByExample(scs).getResults();
//		tr=tree(list);
//		AjaxUtil.printJSonArr(response, tr);
//	}
//	
//    /**
//     * 自调函数 生成tree数据
//     **/
//	private String  tree(List<BasicMaterialsDO> list) throws SyBaseException {
//		StringBuffer sf =new StringBuffer();
//		sf.append("[");
//		for(int i=0;i<list.size();i++){
//			sf.append("{");
//			sf.append("\"text\":").append("\"").append(list.get(i).getCode()).append("\"").append(",").append("\"id\":").append("\"").append(list.get(i).getId()).append("\"");
//			if(i==(list.size()-1)){
//				sf.append("}");
//			}else{
//				sf.append("},");
//			}
//		}
//		sf.append("]");
//		return sf.toString();
//	}
//	
//	@RequestMapping(value = "/unitlist")
//	public void hublist1(HttpServletRequest request,HttpServletResponse response,Model model)throws Exception{
//		//String id = "0a8c3a3c-34f5-48d8-a4d0-303ec8e29807";
//		String id = request.getParameter("id");
//		List<BasicMeasurementUnitDO> bpfd=null;
//		try{
//			bpfd=	basicMeasurementUnitService.findUnit(id);
//		}catch(Exception e){
//			e.printStackTrace();
//		}
////		return EIDT;
//		AjaxUtil.printJSonArr(response, JSONArray.toJSONString(bpfd).toString());
//	}
//	
//	@RequestMapping(value = "/getUnitconvert")
//	public void polist(HttpServletRequest request,HttpServletResponse response,Model model)throws Exception{
//		    String id = request.getParameter("id");
//			String code = request.getParameter("code");
//			String unit = request.getParameter("unit");
//			BasicMeasurementUnitDO bDo = new BasicMeasurementUnitDO();
//			bDo.setMaterialsid(id);
//			bDo.setMaterialscode(code);
//			bDo.setUnitname(unit);
//			List<BasicMeasurementUnitDO> bDos= basicMeasurementUnitService.findListByExample(bDo);
//			AjaxUtil.printJSonArr(response, JSONArray.toJSONString(bDos).toString());
//	}
//}
